package com.mcxx.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.system.service.BlacklistService;
import com.mcxx.modules.miniSalvation.fundpay.param.BlacklistParam;
import com.mcxx.modules.miniSalvation.order.dto.AddBlacklistRecordDTO;
import com.mcxx.modules.miniSalvation.order.service.read.BlacklistReadService;
import com.mcxx.modules.miniSalvation.order.service.write.BlacklistWriteService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@Validated
@Api("数据管理/黑名单管理")
@RestService({"dataManage/blacklistManage/v2"})
public class BlacklistController extends DefaultRestService {

    private final static Logger logger = LoggerFactory.getLogger(BlacklistController.class);

    @Autowired
    private BlacklistService blacklistService;
    @Autowired
    private BlacklistReadService blacklistReadService;
    @Autowired
    private BlacklistWriteService blacklistWriteService;


    @PostMapping("addRecord")
    @ApiOperation("加入黑名单")
    public void addRecord(@RequestBody AddBlacklistRecordDTO dto) {
        logger.info("正在加入黑名单，参数：{}", JSON.toJSONString(dto));
        blacklistService.addRecord(dto);
        logger.info("加入黑名单成功");
    }


    @PostMapping("blackList")
    @ApiOperation("黑名单列表")
    public void blackList(BlacklistParam param, PageParam pageParam) {
        try {
            Page page = blacklistReadService.blackList(param, pageParam,getCurUserAccount());
            setPageBean(page);
        } catch (BizException e) {
            setError(e.getErrorMessage());
        }
    }

    @PostMapping("delete")
    @ApiOperation("移除黑名单")
    public void delete(String ids) {
        try {
            if (ids.endsWith(",")) {
                ids = ids.substring(0, ids.lastIndexOf(","));
            }
            int res = blacklistWriteService.deleteByIds(ids,getCurUserAccount());
            if (res == 0) {
                setError(FailureCode.ERR_30006.getMsg());
            }
        } catch (BizException e) {
            setError(e.getErrorMessage());
        }
    }
}
